import type { LocaleType } from '/#/local';

import { defineStore } from 'pinia';
import { store } from '@/store';

const LOCALE = uni.getStorageSync('locale') || 'zh-CN';

export const useLocaleStore = defineStore({
  id: 'app-locale',
  state: () => ({
    // 国际化
    locale: LOCALE,
  }),
  getters: {
    // 国际化
    getLocale(): LocaleType {
      return this.locale;
    },
  },
  actions: {
    setLocale(val) {
      this.locale = val;
      uni.setStorageSync('locale', val);
    },
  },
});

// Need to be used outside the setup
export function useLocaleStoreWithOut() {
  return useLocaleStore(store);
}
